package com.uchengwang.ui.cm_ui.uu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.uu.bean.ChatNoticeBean;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2018/4/17.
 */

public class XiangliaoRelationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
//    private int[] resIds = new int[]{R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5};
//    private String[] names = new String[]{"至尊宝", "紫霞仙子", "白晶晶", "铁扇公主", "孙悟空", "唐僧", "女儿国王"};
//
//    private Random random = new Random();
    private List<ChatNoticeBean> data = new ArrayList<>();

    public XiangliaoRelationAdapter(Context context){
        this.context = context;
    }

    public void setData(List<ChatNoticeBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public List<ChatNoticeBean> getData(){
        return data;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_relation_item, parent, false);
        return new RelationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        RelationViewHolder holder = (RelationViewHolder) h;

        final ChatNoticeBean bean = data.get(position);

        if (bean.getFromUid() ==UCommonUtil.getUtil().getId()){
            //说明这个招呼是自己打的
            ImageManager.getInstance().loadImage(context, holder.relationItem_head, bean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            if (!TextUtils.isEmpty(bean.getRemarkName())){
                holder.relationItem_name.setText(bean.getRemarkName());
            }else {
                holder.relationItem_name.setText(bean.getNickName());
            }
            holder.relationItem_bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UCommonUtil.getUtil().toChatActiviy(context, bean.getUid(), Conversation.ConversationType.PRIVATE, bean.getNickName());
                }
            });
            holder.relationItem_content.setText("您打了招呼");
        }else {
            //说明这个招呼是别人主动打的
            ImageManager.getInstance().loadImage(context, holder.relationItem_head, bean.getFromUserAvatar(), ImageManager.ImageType.CIRCLE);
            if (!TextUtils.isEmpty(bean.getRemarkName())){
                holder.relationItem_name.setText(bean.getRemarkName());
            }else {
                holder.relationItem_name.setText(bean.getFromNickName());
            }


            holder.relationItem_bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UCommonUtil.getUtil().toChatActiviy(context, bean.getFromUid(), Conversation.ConversationType.PRIVATE, bean.getFromNickName());
                }
            });
            holder.relationItem_content.setText("ta打了招呼");
        }

        if (!bean.isRead()){
            holder.relationItem_msg.setVisibility(View.VISIBLE);
        }else {
            holder.relationItem_msg.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class RelationViewHolder extends RecyclerView.ViewHolder {
        private ImageView relationItem_head;
        private TextView relationItem_msg, relationItem_name, relationItem_content;
        private Button relationItem_bt;
        public RelationViewHolder(View view) {
            super(view);
            AutoUtils.auto(view);

            relationItem_head = view.findViewById(R.id.relationItem_head);
            relationItem_msg = view.findViewById(R.id.relationItem_msg);
            relationItem_name = view.findViewById(R.id.relationItem_name);
            relationItem_content = view.findViewById(R.id.relationItem_content);
            relationItem_bt = view.findViewById(R.id.relationItem_bt);
        }
    }
}
